package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2024-01-13
 * Time: 12:32
 */
public class MySingleList {
    static class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;//头节点

    /**
     * 手动创建一个测试链表
     */
    public void createLink() {
        Node node1 = new Node(12);
        Node node2 = new Node(23);
        Node node3 = new Node(34);
        Node node4 = new Node(45);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }
    /**
     * 遍历链表
     */
    public void display() {
        Node p = this.head;
        while (p != null) {
            System.out.print(p.val + " ");
            p = p.next;
        }
    }
    public boolean contains(int key) {
        Node p = this.head;
        while (p != null) {
            if (p.val == key){
                return true;
            }
            p = p.next;
        }
        return false;
    }
    public int size() {
        Node p = this.head;
        int count = 0;
        while (p != null) {
            count++;
            p = p.next;
        }
        return count;
    }
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = this.head;
        head = node;
    }
    public void addLast(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
            return;
        }
        Node p = this.head;
        while (p.next != null) {
            p = p.next;
        }
        p.next = node;
    }
    private boolean checkIndex(int pos) {
        if (pos < 0 || pos > size()) {
            return false;
        }
        return true;
    }
    public void addIndex(int pos, int data) throws IndexOutOfException{
        if (!this.checkIndex(pos)) {
            throw new IndexOutOfException("位置不合法，请检查");
        }
        if (pos == 0) {
            addFirst(data);
            return;
        }
        if (pos == this.size()) {
            addLast(data);
            return;
        }
        Node node = new Node(data);
        Node p = this.head;
        while (pos-1 > 0) {
            p = p.next;
            pos--;
        }
        node.next = p.next;
        p.next = node;
    }
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        Node p = searchPrev(key);
        if (p == null) {
            return;
        }
        Node del = p.next;
        p.next = del.next;
    }
    private Node searchPrev(int key) {
        Node p = this.head;
        while (p.next != null) {
            if (p.next.val == key) {
                return p;
            }
            p = p.next;
        }
        return null;
    }
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }
//        while (this.head.val == key) {
//            this.head = this.head.next;
//        }
        Node prev = this.head;
        Node cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
            }else {
                prev = cur;
            }
            cur = cur.next;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
        }
    }
    public void clear() {
        this.head = null;
    }
}
